package com.owen.websocket.socketio.controller;

import com.corundumstudio.socketio.SocketIOServer;
import com.owen.websocket.socketio.service.SocketIOService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/socketio")
public class SocketIOController {

    @Resource
    private SocketIOService socketIOService;

    @Resource
    private SocketIOServer socketIOServer;

    @RequestMapping("/pushMessageToUser")
    public String pushMessageToUser(String userId,String content){
        socketIOService.pushMessageToUser(userId,content);
        return "content";
    }

    @RequestMapping("/addEventListener")
    public String addEventListener(String eventName){
        // 自定义事件 client_info_event -> 监听客户端消息
        socketIOServer.addEventListener(eventName,String.class,(client, data, ackSender) -> {
            log.info(eventName + " ************ 客户端：" + data);
        });
        return "content";
    }

}
